/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartupFrame.java
 *
 * Created on Aug 19, 2009, 9:21:55 PM
 */
package com.bff.startup;

import com.bff.bjj.ui.LAFChangeEvent;
import com.bff.bjj.ui.LAFChangeListener;
import com.bff.bjj.ui.LAFPanel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Bill
 */
public class StartupFrame extends javax.swing.JFrame implements LAFChangeListener {

    private boolean mpdSelected;
    private boolean squeezeSelected;

    /** Creates new form StartupFrame */
    public StartupFrame() {
        initComponents();
        ((LAFPanel) lafPanel).addLAFChangeListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        chkboxStartup = new com.bff.bjj.ui.component.BJJCheckbox();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnStartSqueeze = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnStartMPD = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        lafPanel = new LAFPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Select Server Type");
        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(580, 650));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        chkboxStartup.setText("Show this screen on startup.  ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        getContentPane().add(chkboxStartup, gridBagConstraints);

        jLabel1.setText("(Only check this if you use both SqueezeCenter and MPD)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        getContentPane().add(jLabel1, gridBagConstraints);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("SqueezeCenter"));
        jPanel1.setLayout(new java.awt.BorderLayout());

        btnStartSqueeze.setText("Click Here For SqueezeCenter");
        btnStartSqueeze.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartSqueezeActionPerformed(evt);
            }
        });
        jPanel1.add(btnStartSqueeze, java.awt.BorderLayout.SOUTH);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/squeezecenter.png"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                runSqueezeCenter(evt);
            }
        });
        jPanel1.add(jLabel2, java.awt.BorderLayout.PAGE_START);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/squeezebox.jpg"))); // NOI18N
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                runSqueezeCenter(evt);
            }
        });
        jPanel1.add(jLabel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, new java.awt.GridBagConstraints());

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Music Player Daemon (MPD)"));
        jPanel2.setLayout(new java.awt.BorderLayout());

        btnStartMPD.setText("Click Here for the Music Player Daemon (MPD)");
        btnStartMPD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartMPDActionPerformed(evt);
            }
        });
        jPanel2.add(btnStartMPD, java.awt.BorderLayout.CENTER);

        jLabel4.setBackground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/mpd.png"))); // NOI18N
        jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                runMPD(evt);
            }
        });
        jPanel2.add(jLabel4, java.awt.BorderLayout.PAGE_START);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 0);
        getContentPane().add(jPanel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        getContentPane().add(lafPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartSqueezeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartSqueezeActionPerformed
        setSqueezeSelected(true);
        checkProps();
        StartApp.runSqueezeCenter();
        dispose();
    }//GEN-LAST:event_btnStartSqueezeActionPerformed

    private void btnStartMPDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartMPDActionPerformed
        setMpdSelected(true);
        checkProps();
        StartApp.runMPD();
        dispose();
    }//GEN-LAST:event_btnStartMPDActionPerformed

    private void runSqueezeCenter(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_runSqueezeCenter
        setSqueezeSelected(true);
        checkProps();
        StartApp.runSqueezeCenter();
        dispose();
    }//GEN-LAST:event_runSqueezeCenter

    private void runMPD(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_runMPD
        setMpdSelected(true);
        checkProps();
        StartApp.runMPD();
        dispose();
    }//GEN-LAST:event_runMPD
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStartMPD;
    private javax.swing.JButton btnStartSqueeze;
    private javax.swing.JCheckBox chkboxStartup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel lafPanel;
    // End of variables declaration//GEN-END:variables

    private void checkProps() {
        String laf = ((LAFPanel) lafPanel).getSelectedLAF();
        if (chkboxStartup.isSelected()) {
            StartUpProperties.getInstance().saveSettings(StartUpProperties.StartupType.BOTH, laf);
        } else {
            if (isSqueezeSelected()) {
                StartUpProperties.getInstance().saveSettings(StartUpProperties.StartupType.SQUEEZE, laf);
            } else if (isMpdSelected()) {
                StartUpProperties.getInstance().saveSettings(StartUpProperties.StartupType.MPD, laf);
            }
        }
    }

    /**
     * @return the mpdSelected
     */
    public boolean isMpdSelected() {
        return mpdSelected;
    }

    /**
     * @param mpdSelected the mpdSelected to set
     */
    public void setMpdSelected(boolean mpdSelected) {
        this.mpdSelected = mpdSelected;
    }

    /**
     * @return the squeezeSelected
     */
    public boolean isSqueezeSelected() {
        return squeezeSelected;
    }

    /**
     * @param squeezeSelected the squeezeSelected to set
     */
    public void setSqueezeSelected(boolean squeezeSelected) {
        this.squeezeSelected = squeezeSelected;
    }

    public void setBothSelected() {
        chkboxStartup.setSelected(true);
    }

    @Override
    public void lafChanged(LAFChangeEvent event) {
        try {
            UIManager.setLookAndFeel(StartUpProperties.getInstance().getLAFClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StartupFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(StartupFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(StartupFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(StartupFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(this);
        pack();
    }
}
